(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.group.deleteRejectController', deleteController);
	deleteController.$inject = [ '$scope', 'Constants', 'layoutConfig', 'validatorConfig', 'gridService', 'authService', 'modalService', 'groupService' ];
	function deleteController($scope, Constants, layoutConfig, validatorConfig, gridService, authService, modalService, groupService) {
		var ctrl = this, params = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.init = function() {
			ctrl.accessRolePermission = {permission : 'accessRole', authorization : authService};
			ctrl.accessBuPermission =  {permission : 'accessBu', authorization : authService};
			// init group data
			groupService.initPrincipalRoles($scope, ctrl);
			groupService.initParentGroups($scope, ctrl);
			groupService.initSubGroups($scope, ctrl);
			groupService.initGroupUsers($scope, ctrl);
			groupService.initBus($scope, ctrl);
			ctrl.parentGroupGridOptions.enableRowHeaderSelection = false;
			ctrl.principalRoleGridOptions.enableRowHeaderSelection = false;
			ctrl.subGroupGridOptions.enableRowHeaderSelection = false;
			ctrl.userGridOptions.enableRowHeaderSelection = false;
			ctrl.buGridOptions.enableRowHeaderSelection = false;
			authService.deleteReject(params).then(function(response) {
				if (response.data && response.data.model && response.data.task) {
					ctrl.group = response.data.model;
					ctrl.group.noteMsgs = ctrl.group.noteMsgs || [];
					ctrl.group.taskId = response.data.task.taskId;
					ctrl.getPrincipalRoleList();
					ctrl.getParentGroupList();
					ctrl.getSubGroupList();
					ctrl.getGroupUserList();
					groupService.getParentCompanyCodeOptions(ctrl).then(function(response) {
						ctrl.companyCodeOptions = response;
					}, angular.noop);
				}
			});
			
		};
		
		ctrl.getPrincipalRoleList = function() {
			authService.getPrincipalRoleList(ctrl.group).then(function(response) {
				ctrl.group.principalRolesOld = response.data.list || [];
				gridService.updateGridOptions(ctrl.group.principalRolesOld, ctrl.principalRoleOldGridOptions);
			});
		};
		
		ctrl.getParentGroupList = function() {
			authService.getParentGroupList(ctrl.group).then(function(response) {
				ctrl.group.parentGroupsOld = response.data.list || [];
				gridService.updateGridOptions(ctrl.group.parentGroupsOld, ctrl.parentGroupOldGridOptions);
			});
		};
		
		ctrl.getSubGroupList = function() {
			authService.getSubGroupList(ctrl.group).then(function(response) {
				ctrl.group.subGroupsOld = response.data.list || [];
				gridService.updateGridOptions(ctrl.group.subGroupsOld, ctrl.subGroupOldGridOptions);
			});
		};
		
		ctrl.getGroupUserList = function() {
			authService.getGroupUserList(ctrl.group).then(function(response) {
				ctrl.group.groupUsersOld = response.data.list || [];
				gridService.updateGridOptions(ctrl.group.groupUsersOld, ctrl.userOldGridOptions);
			});
		};

		
		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedMessages = selectedRows;
		}
		
		ctrl.deleteRejectSubmit = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.delete', args : [ctrl.group.primaryKey]} ],
				'btnCallback' : {
					'ok' : function(response) {
						authService.deleteRejectSubmit(ctrl.group).then(deleteSuccessful, deleteFailed);
					}
				}
			});
		};
		
		ctrl.deleteRejectCancel = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.cancel', args : [ctrl.group.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.deleteRejectCancel(ctrl.group).then(cancelSuccessful, cancelFailed);
					}
				}
			});
		};
		
		function deleteSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.delete' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.group.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.group.list',
					'state' : 'cda.group.list',
					'params' : {}
				}]
			});
		}

		function deleteFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		function cancelSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.cancel' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.group.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.group.list',
					'state' : 'cda.group.list',
					'params' : {}
				}]
			});
		}

		function cancelFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);